home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************
- *
- * $Source: /unixb/home/unixlib/source/unixlib37/src/unix/c/RCS/dup2,v $
- * $Date: 1996/10/30 21:59:01 $
- * $Revision: 1.2 $
- * $State: Rel $
- * $Author: unixlib $
- *
- * $Log: dup2,v $
- * Revision 1.2 1996/10/30 21:59:01 unixlib
- * Massive changes made by Nick Burret and Peter Burwood.
- *
- * Revision 1.1 1996/04/19 21:35:27 simon
- * Initial revision
- *
- ***************************************************************************/
-
- static const char rcs_id[] = "$Id: dup2,v 1.2 1996/10/30 21:59:01 unixlib Rel $";
-
- #include <string.h>
- #include <errno.h>
- #include <unistd.h>
-
- #include <sys/types.h>
- #include <sys/unix.h>
- #include <sys/os.h>
-
- int
- dup2 (int fd1, int fd2)
- {
- register struct file *f1, *f2;
-
- if (BADF (fd1) || (fd2 >= MAXFD) || (fd2 < 0))
- {
- errno = EBADF;
- return (-1);
- }
-
- if (fd1 == fd2)
- return (0);
-
- f1 = __u->file + fd1;
- f2 = __u->file + fd2;
-
- if (f2->dup)
- close (fd2);
-
- memcpy (f2, f1, sizeof (struct file));
-
- f2->dup = f1;
-
- while (f1->dup != f2->dup)
- f1 = f1->dup;
-
- f1->dup = f2;
-
- return (0);
- }
-